package com.zx.crowdfunding.handler;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zx.crowdfunding.api.MySQLRemoteService;
import com.zx.crowdfunding.constant.CrowdFundingConstant;
import com.zx.crowdfunding.entity.vo.AddressVO;
import com.zx.crowdfunding.entity.vo.MemberLoginVO;
import com.zx.crowdfunding.entity.vo.OrderProjectVO;
import com.zx.crowdfunding.util.ResultEntity;

/**
 * 订单 - Handler
 * @author 郑雪
 * @date 2022-01-04
 */
@Controller
public class OrderConsumerHandler {
	
	/**
	 * MySQL数据服务远程接口
	 */
	@Autowired
	private MySQLRemoteService mysqlRemoteService;
	
	/**
	 * 添加收货地址
	 * @param addressVO 收货地址VO对象
	 * @return 重定向到创建订单第二步，确认订单页面
	 */
	@RequestMapping("/save/address")
	public String saveAddress(AddressVO addressVO, HttpSession session) {
		
		// 调用远程接口添加收货地址
		mysqlRemoteService.saveAddressVORemote(addressVO);
		
		// 从Session域中获取临时的OrderProjectVO对象
		OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(CrowdFundingConstant.ATTR_NAME_TEMPLE_ORDER_PROJECT);
		
		// 从OrderProjectVO对象中获取回报数量
		Integer returnCount = orderProjectVO.getReturnCount();
		
		// 重定向到创建订单第二步，确认订单页面
		return "redirect:/order/confirm/order/info/"+returnCount;
	}
	
	/**
	 * 显示创建订单第二步，确认订单页面
	 * @param returnCount 回报数量
	 * @param session 当前会话
	 * @return 重定向到确认订单页面
	 */
	@RequestMapping("/confirm/order/info/{returnCount}")
	public String toConfirmOrderPage(
				@PathVariable("returnCount") Integer returnCount, 
				HttpSession session
			) {
		
		// 从Session域中获取临时的OrderProjectVO对象
		OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute(CrowdFundingConstant.ATTR_NAME_TEMPLE_ORDER_PROJECT);
		
		// 给OrderProjectVO对象中的回报数量属性赋值
		orderProjectVO.setReturnCount(returnCount);
		
		session.setAttribute(CrowdFundingConstant.ATTR_NAME_TEMPLE_ORDER_PROJECT, orderProjectVO);
		
		// 当前登录的会员
		MemberLoginVO memberLoginVO = (MemberLoginVO) session.getAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_MEMBER);
		
		// 当前登录的会员id
		Integer memberId = memberLoginVO.getId();
		
		// 调用远程接口查询收货地址数据列表
		ResultEntity<List<AddressVO>> resultEntity = mysqlRemoteService.getAddressVORemote(memberId);
		
		if(ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
			
			// 获取查询到的数据
			List<AddressVO> addressVOList = resultEntity.getData();
			
			// 将数据存入Session域中
			session.setAttribute("addressVOList", addressVOList);
			
		}
		
		// 重定向到确认订单页面
		return "order-confirm";
	}
	
	/**
	 * 显示创建订单第一步，确认回报页面
	 * @param returnId 项目回报id
	 * @return 跳转到创建订单第一步，确认回报内容页面
	 */
	@RequestMapping("/confirm/return/info/{returnId}")
	public String toConfirmReturnPage(
				@PathVariable("returnId") Integer returnId, 
				HttpSession session) {
		
		// 调用远程接口查询订单项目
		ResultEntity<OrderProjectVO> resultEntity = mysqlRemoteService.getOrderProjectVORemote(returnId);
		
		// 检查查询结果
		String result = resultEntity.getResult();
		
		// 如果成功
		if(ResultEntity.SUCCESS.equals(result)) {
			
			// 获取查询到的数据
			OrderProjectVO orderProjectVO = resultEntity.getData();
			
			// 将数据存入Session域中
			session.setAttribute(CrowdFundingConstant.ATTR_NAME_TEMPLE_ORDER_PROJECT, orderProjectVO);
			
		}
		
		// 跳转到创建订单第一步，确认回报内容页面
		return "order-return";
	}
	
}
